package com.antoco.controlcenter.comm;

public class PropertyBase {
    private String propertyName;
    private String propertyType;
    private Object propertyValue;
    //display
    private boolean isDisplay = false;
    private String displayTemplate;
    //parser
    private boolean isParser = false;
    private int parserOffset;
    private int parserLength;
    private String parserType;
    private String parserMeta; //元数据
    private long parserMask;
    private double parserAccuracy;
    private double parserCorrect;

    public boolean getIsDisplay() {
        return this.isDisplay;
    }

    public boolean getIsParser() {
        return this.isParser;
    }

    public String getName() {
        return this.propertyName;
    }

    public Object getValue() {
        return this.propertyValue;
    }

    public void setValue(Object value) {
        if (this.propertyType.equals("Number")) {
            if (value instanceof Double) {
                this.propertyValue = value;
            }
        }
    }

    public void setNumberValue(Double value) {
        this.propertyValue = value;
    }

    public void setNumberValue(String value) {
        this.propertyValue = Double.parseDouble(value);
    }

    public double getNumberValue() {
        return (double) this.propertyValue;
    }

    public PropertyBase(String name, String type) {
        this.propertyName = name;
        this.propertyType = type;
    }

    public PropertyBase(String name, String type, Object value) {
        this.propertyName = name;
        this.propertyType = type;
        this.propertyValue = value;
    }

    public void initDisplay(String template) {
        this.displayTemplate = template;
        this.isDisplay = true;
    }

    public void initParser(int offset, int length) {
        this.parserOffset = offset;
        this.parserLength = length;
        this.parserMask = 0xFFFFFFFF;
        this.parserAccuracy = 1;
        this.parserCorrect = 0;
        this.isParser = true;
    }

    public void initParser(int offset, int length, String type, long mask, double accuracy, double correct, String parserMeta) {
        this.parserOffset = offset;
        this.parserLength = length;
        this.parserType = type;
        this.parserMask = mask;
        this.parserAccuracy = accuracy;
        this.parserCorrect = correct;
        this.isParser = true;
    }

    public void parserValue(byte[] buffer) {
        if (this.propertyType.equals("Number")) {
            if (!this.isParser) return;
            if (this.parserLength > 4) return;

            long temp = 0;
            //大端
            for (int i = 0; i < this.parserLength; i++) {
                temp <<= 8;
                temp |= buffer[this.parserOffset + this.parserLength - 1 - i] & 0xFF;
            }
            //小端
//        for(int i=0;i<this.parserLength;i++){
//            value <<= 8;
//            value |= buffer[this.parserOffset+i]&0xFF;
//        }
            temp &= this.parserMask;
            double value;
            switch (this.parserType) {
                case "s8":
                    value = (byte) temp;
                    break;
                case "s16":
                    value = (short) temp;
                    break;
                default:
                    value = temp;
                    break;
            }

            this.propertyValue = value * this.parserAccuracy + this.parserCorrect;
        }

    }

    public String getDisplay() {
        if (propertyValue != null) {
            return String.format(this.displayTemplate, (double) this.propertyValue);
        }
        return "";
    }
}
